<template>
  <div class="app-container page-wood-purchase-record">
    <FilterForm :data="filterData" @search="getList" labelWidth="86px" />
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:salesReturn:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:salesReturn:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
         <el-button
          type="warning"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleCheckOrder(1)"
          v-hasPermi="['business:salesReturn:edit']"
        >审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleCheckOrder(0)"
          v-hasPermi="['business:salesReturn:edit']"
        >反审核</el-button>
      </el-col>
   
    </el-row>

    <Table
        :api="$Api.sales.return.list"
        :selection="true"
        @select="handleSelectionChange"
        @dbClick="rowDblclick"
        ref="Table"
        height="60vh"
        >
      <el-table-column label="操作" align="center" width="130"   fixed="left">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click.native.stop="handleUpdate(scope.row)"
            v-hasPermi="['woodPurchase:purchaseOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click.native.stop="handleDelete(scope.row)"
            v-hasPermi="['woodPurchase:purchaseOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="单据日期" align="center" prop="billTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户" align="center" prop="customerName" width="120"/>
      <el-table-column label="方数" align="center" prop="salesCount" />
      <el-table-column label="金额" align="center" prop="money" />
      <el-table-column label="单据编号" align="center" prop="returnId" width="160"/>
      <el-table-column label="关联出库单号" align="center" prop="deliveryId" width="155"/>
      <el-table-column label="销售员" align="center" prop="salesName" width="160"/>
      <el-table-column label="制单人" align="center" prop="createBy" />
      <el-table-column label="审核状态" align="center" key="auditStatus" fixed="right">
        <template slot-scope="scope">
         <span class="success" v-if="scope.row.auditStatus == 1">已审核</span>
         <span class="error" v-else>未审核</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditor" fixed="right"/>
      <el-table-column label="备注" align="center" prop="remark" />
    </Table>

  </div>
</template>

<script>


export default {
  name: "PurchaseOrder",
    components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 选中状态
      status:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pactNum: null,
        ladingNum: null,
        supplierName: null,
        pactId: null,
        pactName: null,
        harbor: null,
        auditStatus: null,
        arrayOrderStatus: [],
        paymentClause: null,
        deliveryType: null,
        shippingTime: null,
        beginRegisterTime:"",
        endRegisterTime:""
  
      },

      filterData: [
                {
                    label: "单据日期",
                    type: "period_date",
                    st: "beginBillTime",
                    end: "endBillTime",
                    scale: 2,
                },
                {
                    label: "客户",
                    name: "customerName",
                    type: "customer",
                },
                { label: "单据编号", name: "returnId" },
                {
                    label: "审核状态",
                    name: "auditStatus",
                    type: "select",
                    select:{
                      option:[{
                              value: '1',
                              label: '已审核'
                            }, {
                              value: '0',
                              label: '未审核'
                            }
                          ],
                        selectLabel: "label",
                        selectValue: "value",
                        multiple:true
                    },
                },
            ],
  
      // 表单参数   
      form: {},
      // 表单校验
      rules: {
        delFlag: [
          { required: true, message: "删除标志;不能为空", trigger: "blur" }
        ],
      }
    };
  },
  computed:{

  },
  created() {
    
  },
  methods: {
    /** 查询采购订单信息列表 */
    getList(params) {
            this.$refs.Table.getData(params);
        },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.returnId)
      this.status = selection.map(item => item.auditStatus)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
       this.$router.push({
        path: "/sale/receipts/sales-return",
      });
    },
    // 双击操作
    rowDblclick(row, column, event) {
      this.handleUpdate(row);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.returnId;
      let path = `/sales/salesReturn/list/${id}`;
      this.$router.push({
        path,
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let state = false
      let { status } = this
      for( let i = 0; i < status.length; i++) {
        if( status[i] == 1) {
          state = true
        }
      }
      if(row.auditStatus == 1 || state == true) {
        this.$message.error('已审核，不能删除');
      }else {
        const returnIds = row.returnId || this.ids;
        this.$confirm('是否确认删除采购订单信息编号为"' + returnIds + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() =>{
            this.$Api.sales.return.del(returnIds).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          }).catch(() => {});
          })
        }
    },
    // 审核订单 审核与反审核 审核状态;0未审核 1已审核
    handleCheckOrder(isStatus){
      let { ids } = this;
      this.$confirm('是否确认'+`${isStatus == "1" ? '审核' : '反审核'}`+'采购订单信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.$Api.sales.return.setState({
            ids: ids,
            auditStatus:isStatus,
            isStatus: 0,
        }).then( (res) =>{
          if(res.code == 200){
              this.getList();
              this.msgSuccess( `${isStatus == "1" ? '审核' : '反审核'}`+"成功");
          }
        }).catch(() => {
           this.getList();
        });  
        })
    },
  }
};
</script>

<style lang="scss" scoped>
  .page-wood-purchase-record {
  .success{
    color: #67C23A;
  }
  .error{
    color: #F56C6C;
  }
  .disabled {
    color: #a8a8a8;
  }

  .error {
    color: #fa3e3e;
  }

    .el-table {
    margin: 0 15px;
    width: calc(100% - 30px);
    border: 1px solid #eaeaea;
    border-radius: 5px;

    .cell {
      font-size: 12px;
      cursor: pointer;
      text-align: center;
    }
    .el-input--medium {
      .el-input__inner {
        height: 24px;
        line-height: 24px;
      }
      .el-input__icon {
        line-height: 24px;
      }
    }
    td {
      padding: 3px;
      & + td {
        border-left: 1px solid #eaeaea;
      }
    }
    .el-table__body {
      td {
        padding: 5px 0;
        .el-button--mini.is-circle {
          padding: 5px;
        }
      }
    }
  }
}
</style>